unit clsFase_Assistencia;

interface

uses Dm, dbtables, biblio, Grids;

type
  TFase_Assistencia = class(TObject)
  public
    Pro_Codigo: string;
    Pro_Codigo1: string;
    function Recuperar(Ass_Numero, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item: string): boolean;
    function Recuperar1(Ass_Numero, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item, Sequencia: string): boolean;
    function Existe_Item(Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item: string): boolean;
    function Soma_Qtde(Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item: string): double;
    function Produto_Completo(Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item: string): boolean;
    function Excluir1(Ass_Numero, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item, cod_produto: string): string;
    procedure Gravar(Ass_Numero, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item, Sequencia: string);
    procedure Excluir_Assistencia(Ass_Numero: string);
    procedure Excluir(Ass_Numero, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item, Sequencia: string);
    procedure Consultar(Ass_Numero, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item: string; var tq: TQuery);
  end;

implementation

uses clsItemNotaVenda, clsFicha, clsParametros;

var
  ficha: TFicha;
  item: TItemNotaVenda;
  parametros: TParametros;

procedure TFase_Assistencia.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into Fase_Assistencia values (' +
    ':k_Ass_Numero ,' +
    ':k_Nota_Serie ,' +
    ':k_Nota_Numero,' +
    ':k_Ent_Codigo ,' +
    ':k_Itnv_Item  ,' +
    ':k_Sequencia  ,' +
    ':k_Pro_codigo ,' +
    ':k_Pro_Codigo1)');
  tq.parambyname('k_Ass_Numero').asstring := Ass_Numero;
  tq.parambyname('k_Nota_Serie').asstring := Nota_Serie;
  tq.parambyname('k_Nota_Numero').asstring := Nota_Numero;
  tq.parambyname('k_Ent_Codigo').asstring := Ent_Codigo;
  tq.parambyname('k_Itnv_Item').asstring := Itnv_Item;
  tq.parambyname('k_Sequencia').asstring := Sequencia;
  tq.parambyname('k_Pro_codigo').asstring := Pro_codigo;
  tq.parambyname('k_Pro_codigo1').asstring := Pro_codigo1;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TFase_Assistencia.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Fase_Assistencia where Ass_Numero=:a and Nota_Serie=:b and ' +
    'Nota_Numero=:c and Ent_Codigo=:d and Itnv_Item=:e and Sequencia=:f');
  tq.params[0].asstring := Ass_Numero;
  tq.params[1].asstring := Nota_Serie;
  tq.params[2].asstring := Nota_Numero;
  tq.params[3].asstring := Ent_Codigo;
  tq.params[4].asstring := Itnv_Item;
  tq.params[5].asstring := Sequencia;
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TFase_Assistencia.Excluir1;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Fase_Assistencia where Ass_Numero=:a and Nota_Serie=:b and ' +
    'Nota_Numero=:c and Ent_Codigo=:d and Itnv_Item=:e and Pro_Codigo=:f');
  tq.params[0].asstring := Ass_Numero;
  tq.params[1].asstring := Nota_Serie;
  tq.params[2].asstring := Nota_Numero;
  tq.params[3].asstring := Ent_Codigo;
  tq.params[4].asstring := Itnv_Item;
  tq.params[5].asstring := cod_produto;
  tq.open;
  if tq.eof then result := '@&^' else result := tq.fieldbyname('Sequencia').asstring;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Fase_Assistencia where Ass_Numero=:a and Nota_Serie=:b and ' +
    'Nota_Numero=:c and Ent_Codigo=:d and Itnv_Item=:e and Pro_Codigo=:f');
  tq.params[0].asstring := Ass_Numero;
  tq.params[1].asstring := Nota_Serie;
  tq.params[2].asstring := Nota_Numero;
  tq.params[3].asstring := Ent_Codigo;
  tq.params[4].asstring := Itnv_Item;
  tq.params[5].asstring := cod_produto;
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TFase_Assistencia.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Fase_Assistencia where Ass_Numero=:a and Nota_Serie=:b and ' +
    'Nota_Numero=:c and Ent_Codigo=:d and Itnv_Item=:e');
  tq.params[0].asstring := Ass_Numero;
  tq.params[1].asstring := Nota_Serie;
  tq.params[2].asstring := Nota_Numero;
  tq.params[3].asstring := Ent_Codigo;
  tq.params[4].asstring := Itnv_Item;
  tq.open;
  if tq.eof then result := false else result := true;
  tq.close;
  tq.free;
end;

function TFase_Assistencia.Recuperar1;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Fase_Assistencia where Ass_Numero=:a and Nota_Serie=:b and ' +
    'Nota_Numero=:c and Ent_Codigo=:d and Itnv_Item=:e and Sequencia=:f');
  tq.params[0].asstring := Ass_Numero;
  tq.params[1].asstring := Nota_Serie;
  tq.params[2].asstring := Nota_Numero;
  tq.params[3].asstring := Ent_Codigo;
  tq.params[4].asstring := Itnv_Item;
  tq.params[5].asstring := Sequencia;
  tq.open;
  if tq.eof then result := false else result := true;
  Pro_Codigo := tq.fieldbyname('Pro_Codigo').asstring;
  Pro_Codigo1 := tq.fieldbyname('Pro_Codigo1').asstring;
  tq.close;
  tq.free;
end;

procedure TFase_Assistencia.Consultar;
begin
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Fase_Assistencia where Ass_Numero=:a and Nota_Serie=:b and ' +
    'Nota_Numero=:c and Ent_Codigo=:d and Itnv_Item=:e');
  tq.params[0].asstring := Ass_Numero;
  tq.params[1].asstring := Nota_Serie;
  tq.params[2].asstring := Nota_Numero;
  tq.params[3].asstring := Ent_Codigo;
  tq.params[4].asstring := Itnv_Item;
  tq.open;
end;

function TFase_Assistencia.Existe_Item;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Fase_Assistencia where Nota_Serie=:b and Nota_Numero=:c and ' +
    'Ent_Codigo=:d and Itnv_Item=:e');
  tq.params[0].asstring := Nota_Serie;
  tq.params[1].asstring := Nota_Numero;
  tq.params[2].asstring := Ent_Codigo;
  tq.params[3].asstring := Itnv_Item;
  tq.open;
  if not tq.eof then result := true else result := false;
  tq.close;
  tq.Free;
end;

function TFase_Assistencia.Soma_Qtde;
var
  tq: TQuery;
  xqtde: double;
  achou: boolean;
  int: integer;
  res: double;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Item_Assistencia where Nota_Serie=:b and Nota_Numero=:c and ' +
    'Ent_Codigo=:d and Itnv_Item=:e');
  tq.params[0].asstring := Nota_Serie;
  tq.params[1].asstring := Nota_Numero;
  tq.params[2].asstring := Ent_Codigo;
  tq.params[3].asstring := Itnv_Item;
  tq.open;
  xqtde := 0;
  achou := false;
  ficha := TFicha.Create;
  while not tq.eof do begin
    achou := true;
    Recuperar1(tq.fieldbyname('Ass_Numero').asstring, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item, tq.fieldbyname('Sequencia').asstring);
    ficha.Recuperar(Pro_Codigo1, Pro_Codigo);
    if ficha.Fic_Quantidade <> 0 then
      res := tq.fieldbyname('Ass_Quantidade').asfloat / ficha.Fic_Quantidade
    else
      res := tq.fieldbyname('Ass_Quantidade').asfloat;
    int := iv(res);
    if res > int then int := int + 1;
    if int > xqtde then xqtde := int;
    tq.next;
  end;
  if (achou) and (xqtde = 0) then xqtde := 1;
  result := xqtde;
  ficha.free;
  tq.close;
  tq.Free;
end;

function TFase_Assistencia.Produto_Completo;
var
  tq, tq1: TQuery;
  completo: boolean;
  prod: string;
  fase: string;
  vetor: array of string;
  i, pos, j: integer;
  achou: boolean;
begin
  tq1 := Tquery.Create(dmint);
  tq1.databasename := dmint.db.databasename;
  tq1.active := False;
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Item_Assistencia where Nota_Serie=:b and Nota_Numero=:c and ' +
    'Ent_Codigo=:d and Itnv_Item=:e');
  tq.params[0].asstring := Nota_Serie;
  tq.params[1].asstring := Nota_Numero;
  tq.params[2].asstring := Ent_Codigo;
  tq.params[3].asstring := Itnv_Item;
  tq.open;
  completo := true;
  parametros := TParametros.Create;
  parametros.Recuperar('1');
  fase := parametros.Fase_Nivel;
  parametros.free;
  ficha := TFicha.Create;
  item := TItemNotaVenda.Create;
  item.Recuperar(Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item);
  prod := item.Pro_codigo;
  item.free;
  ficha.Recuperar_Fase(prod, fase, tq1);
  setlength(vetor, tq1.RecordCount);
  i := 0;
  while not tq1.eof do begin
    vetor[i] := tq1.fieldbyname('Produto').asstring;
    i := i + 1;
    tq1.next;
  end;
  tq1.close;
  tq1.free;
  while not tq.eof do begin
    Recuperar1(tq.fieldbyname('Ass_Numero').asstring, Nota_Serie, Nota_Numero, Ent_Codigo, Itnv_Item, tq.fieldbyname('Sequencia').asstring);
    ficha.Recuperar(Pro_Codigo1, Pro_Codigo);
    if tq.fieldbyname('Ass_Quantidade').asfloat <> ficha.Fic_Quantidade then completo := false;
    achou := false;
    pos := 0;
    for i := 0 to length(vetor) - 1 do begin
      if vetor[i] = Pro_Codigo then begin
        achou := true;
        pos := i;
      end;
    end;
    if achou then begin
      i := pos;
      for j := pos + 1 to length(vetor) - 1 do begin
        vetor[i] := vetor[j];
        i := i + 1;
      end;
      setlength(vetor, length(vetor) - 1);
    end;
    tq.next;
  end;
  if length(vetor) > 0 then completo := false;
  result := completo;
  ficha.free;
  tq.close;
  tq.Free;
end;

procedure TFase_Assistencia.Excluir_Assistencia;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Fase_Assistencia where Ass_Numero=:a');
  tq.params[0].asstring := Ass_Numero;
  tq.execsql;
  tq.close;
  tq.Free;
end;

end.

